package zz.app.jmhviewer;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.PointF;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ScrollView;



/**
 * Created by zz on 2018/2/1 15:57.
 */

public class KashiScrollView extends ScrollView {
    private Context context;
    private boolean clickState;
    private PointF clickPoint;
    private boolean autoScroll;
    private Handler handler;
    private AutoPlayRunnable runnable;
    private float left,top;
    private OnScrollScaleListener scrollScaleListener;
    private float fingersdis;
    private float fingerspressdis;
    private static final String TAG="TAG";

    public KashiScrollView(Context context) {
        super(context);
        init(context);
    }
    public boolean getAutoScroll(){
        return autoScroll;
    }

    private void init(Context c) {
        context=c;
        autoScroll=true;
    }
    public void setOnScrollScaleListener(OnScrollScaleListener l){
        scrollScaleListener=l;
    }

    public KashiScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }
    public void setHandler(Handler h){
        handler=h;
    }

    @Override
    public boolean performClick() {
        return super.performClick();
    }


    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            clickState =true;
            clickPoint=new PointF(ev.getX(),ev.getY());
        }
        if(ev.getAction()==MotionEvent.ACTION_UP){
            if(clickState){
                if(clickPoint!=null){
                    if(Math.abs(ev.getX()-clickPoint.x)<10&&
                            Math.abs(ev.getY()-clickPoint.y)<10)
                    {
                        performClick();
                    }
                }
            }
            clickPoint=null;
            clickState =false;
        }
        if(ev.getAction()==MotionEvent.ACTION_MOVE){
            if(ev.getPointerCount()<2) {
                if (clickPoint != null) {
                    if (Math.abs(ev.getX() - clickPoint.x) > 30 &&
                            Math.abs(ev.getY() - clickPoint.y) > 30) {
                        if (handler != null) {
                            autoScroll = false;
                            if (runnable != null)
                                handler.removeCallbacks(runnable);
                            runnable = new AutoPlayRunnable(() -> autoScroll = true);
                            handler.postDelayed(runnable, 2000);
                        }
                    }
                }
            }

            else{
                float x0 = ev.getX(0);
                float y0 = ev.getY(0);
                float x1 = ev.getX(1);
                float y1 = ev.getY(1);
                fingersdis = (float) Math.sqrt((x0 - x1) * (x0 - x1) + (y0 - y1) * (y0 - y1));
                if (Math.abs(fingersdis - fingerspressdis) > 300) {
                    if (fingersdis > fingerspressdis) {
                        if (scrollScaleListener != null)
                            scrollScaleListener.onScrollScale(true);
                    } else {
                        if (scrollScaleListener != null)
                            scrollScaleListener.onScrollScale(false);
                    }
                    Log.i(TAG, "onTouchEvent: fingersdis="+fingersdis+" fingerspressdis="+fingerspressdis);
                    fingerspressdis = fingersdis;
                }
                return true;



            }
        }
        if(ev.getActionMasked()==MotionEvent.ACTION_POINTER_DOWN) {
            if (ev.getActionIndex() ==1) {
                float x0 = ev.getX(0);
                float y0 = ev.getY(0);
                float x1 = ev.getX(1);
                float y1 = ev.getY(1);
                fingerspressdis = (float) Math.sqrt((x0 - x1) * (x0 - x1) + (y0 - y1) * (y0 - y1));
            }

        }

        return super.onTouchEvent(ev);
    }


    public void scrollToAnim(final int left, final int top, int time) {
        this.left=getScrollX();
        this.top=getScrollY();
        ValueAnimator animator=ValueAnimator.ofFloat(0,1);
        animator.setDuration(time);
        animator.addUpdateListener(animation -> {
            KashiScrollView view=KashiScrollView.this;
            float value=(float)animation.getAnimatedValue();
            view.scrollTo((int)(view.left+(left-view.left)*value),(int)(view.top+(top-view.top)*value));
        });
        animator.start();


    }
}
